package com.ruoyi.market.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.market.domain.VerCoupon;
import com.ruoyi.market.domain.VerUserCouponOrderRelate;
import com.ruoyi.market.domain.dto.*;
import com.ruoyi.market.service.IVerCouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券Controller
 *
 * @author ruoyi
 * @date 2025-09-02
 */
@RestController
@Tag(name = "优惠券接口", description = "提供优惠券操作")
@RequestMapping("/market/coupon")
public class VerCouponController extends BaseController {
    @Autowired
    private IVerCouponService verCouponService;

    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:list')")
    @Operation(summary = "查询优惠券列表", description = "传入优惠券查询对象，返回优惠券列表")
    @GetMapping("/list")
    public TableDataInfo list(VerCoupon verCoupon) {
        startPage();
        List<VerCoupon> list = verCouponService.selectVerCouponList(verCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出优惠券列表", description = "传入优惠券查询对象，返回优惠券列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, VerCoupon verCoupon) {
        List<VerCoupon> list = verCouponService.selectVerCouponList(verCoupon);
        ExcelUtil<VerCoupon> util = new ExcelUtil<VerCoupon>(VerCoupon.class);
        util.exportExcel(response, list, "优惠券数据");
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:query')")
    @Operation(summary = "获取优惠券详细信息", description = "传入优惠券 ID，返回优惠券详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(verCouponService.selectVerCouponById(id));
    }

    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:add')")
    @Operation(summary = "新增优惠券", description = "传入优惠券对象，返回新增结果")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VerCoupon verCoupon) {
        return toAjax(verCouponService.insertVerCoupon(verCoupon));
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:edit')")
    @Operation(summary = "修改优惠券", description = "传入优惠券对象，返回修改结果")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VerCoupon verCoupon) {
        return toAjax(verCouponService.updateVerCoupon(verCoupon));
    }

    /**
     * 删除优惠券
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:remove')")
    @Operation(summary = "删除优惠券", description = "传入优惠券 ID，返回删除结果")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(verCouponService.deleteVerCouponByIds(ids));
    }

    /**
     * 获取优惠券组商品列表
     */
    @Operation(summary = "获取优惠券组商品列表", description = "传入优惠券组查询对象，返回优惠券组商品列表")
    @GetMapping("/couponGroupList")
    public AjaxResult couponGroupList(CouponGroupDto couponGroupDto) {
        List<CouponGroupDto> list = verCouponService.couponGroupList(couponGroupDto);
        return success(list);
    }

    /**
     * 获取用户优惠券列表
     */
    @Operation(summary = "获取用户优惠券列表", description = "传入用户 ID，返回用户优惠券列表")
    @PostMapping("/userCouponList")
    public AjaxResult userCouponList(@RequestParam Long userId) {
        List<userCouponDto> list = verCouponService.userCouponList(userId);
        return success(list);
    }

    /**
     * 根据用户id查看优惠券使用明细表
     */
    @Operation(summary = "根据用户id查看优惠券使用明细表", description = "传入用户 ID，返回用户优惠券使用明细表")
    @PostMapping("/userCouponDetail")
    public AjaxResult userCouponDetail(@RequestParam Long userId) {
        List<VerUserCouponDto> list = verCouponService.getUserCouponDetail(userId);
        return success(list);
    }

    /**
     * 根据优惠券id获取销量最高的前8个商品
     */
    @Operation(summary = "根据优惠券id获取销量最高的前8个商品", description = "传入优惠券 ID 和分页信息，返回销量最高的商品和分页信息")
    @PostMapping("/couponSaleTop8")
    public AjaxResult couponSaleTop8(@RequestBody CouponSaleTopRequestDto request) {
        CouponSaleTopResponseDto response = verCouponService.getCouponSaleTop8WithPagination(request.getCouponId(),
                request.getPagination());
        return success(response);
    }

    /**
     * 根据优惠券id获取分类商品列表
     */
    @Operation(summary = "根据优惠券id获取分类商品列表", description = "传入优惠券 ID，返回分类商品列表")
    @PostMapping("/couponCategoryList")
    public AjaxResult couponCategoryList(@RequestParam Long couponId) {
        List<SkuCouponsCategoryDto> list = verCouponService.getCouponCategoryList(couponId);
        return success(list);
    }

    /**
     * 根据分类id和优惠券id获取不同页数的商品记录
     */
    @Operation(summary = "根据分类id和优惠券id获取不同页数的商品记录", description = "传入分类id和优惠券id，返回不同页数的商品记录")
    @PostMapping("/couponCategoryListPage")
    public AjaxResult couponCategoryListPage(@RequestBody SkuCouponsCategoryRequestDto request) {
        List<SkuCouponsCategoryDto> list = verCouponService.getCouponCategoryListPage(request);
        return success(list);
    }

    /**
     * 获取所有优惠券信息
     */
    @Operation(summary = "获取所有优惠券信息", description = "返回所有优惠券信息")
    @GetMapping("/getAllCoupons")
    public AjaxResult getAllCoupons() {
        List<CouponGroupResponseDto> list = verCouponService.getAllCoupons();
        return success(list);
    }

    /**
     * 获取所有优惠券销量最高的前8个商品
     */
    @Operation(summary = "获取所有优惠券销量最高的前8个商品", description = "传入分页信息，返回所有优惠券销量最高的商品和分页信息")
    @PostMapping("/getAllCouponsSaleTop8")
    public AjaxResult getAllCouponsSaleTop8(@RequestBody AllCouponsSaleTopRequestDto request) {
        AllCouponsSaleTopResponseDto response = verCouponService
                .getAllCouponsSaleTop8WithPagination(request.getPagination());
        return success(response);
    }

    /**
     * 获取所有优惠券组分类商品列表
     */
    @Operation(summary = "获取所有优惠券组分类商品列表", description = "根据优惠券组分类获取商品列表，支持分页")
    @GetMapping("/getAllCouponGroupCategorySkus")
    public AjaxResult getAllCouponGroupCategorySkus() {
        List<SkuCouponsCategoriesDto> list = verCouponService.getAllCouponGroupCategorySkus();
        return success(list);
    }

    /**
     * 根据couponGroupId获取不同页数的商品记录
     */
    @Operation(summary = "根据优惠券组id获取不同页数的商品记录", description = "传入优惠券组id和分页信息，返回不同页数的商品记录")
    @PostMapping("/couponGroupPage")
    public AjaxResult couponGroupPage(@RequestBody CouponGroupPageRequestDto request) {
        List<SkuCouponsCategoryDto> list = verCouponService.getCouponGroupPage(request);
        return success(list);
    }

}
